<template>
<div>
  <div>
    <van-nav-bar
      :title="$route.name"
      @click-left="$router.back()"
      left-text="返回"
      left-arrow
    />
  </div>
  <div>
<van-address-edit
  :address-info="info"
  :area-list="areaList"
  show-postal
  show-delete
  show-set-default
  show-search-result
  :search-result="searchResult"
  :area-columns-placeholder="['请选择', '请选择', '请选择']"
  @save="onSave"
  @delete="onDelete"
  @change-detail="onChangeDetail"
/>
  </div>
</div>
</template>

<script>
import { Toast } from 'vant';
import { areaList } from '@vant/area-data';
import { addressAdd, addressEdit, addressRemove } from '../request/api'

export default {
  data() {
    return {
      id: null,
      info: {},
      areaList,
      searchResult: [],
    };
  },
  methods: {
    onSave(content) {
      let {
        addressDetail:useraddress,
        name:username,
        tel:userphone,
        areaCode:regioncode,
        isDefault:status,
        province,
        city,
        county
      } = content
      status = status ? 1 : 0
      let location = province + city + county
      let addressList = {
        uid: this.$store.getters.getUser.uid,
        useraddress,
        username,
        userphone,
        location,
        regioncode,
        status
      }
      if(this.id) {
        addressAdd(addressList).then(res => {
          if(res.data.code == 200) {
            Toast('添加成功');
            this.$router.back()
          }
        })
      } else {    
        addressList.id = this.id
        addressEdit(addressList).then(res => {
          console.log(res)
          if(res.data.code == 200) {
            Toast('修改成功');
          this.$router.back()
          }
        })
      }
    },
    onDelete() {
      addressRemove({id:this.id}).then(res => {
        if(res.data.code == 200) {
          Toast('删除成功');
          this.$router.back()
        }
      })
      
    },
    onChangeDetail(val) {
      if (val) {
        this.searchResult = [
          {
            name: '黄龙万科中心',
            address: '杭州市西湖区',
          },
        ];
      } else {
        this.searchResult = [];
      }
    },
  },
  mounted() {
    this.id = this.$route.query.id
      this.info = this.$store.getters.getAddressList.filter(item => {
      if(this.id == item.id) {
        return item
      }
    })[0]
    console.log(this.id)
  }
};

</script>

<style scoped></style>
